package gui;

import java.util.ArrayList;
import java.util.concurrent.CountDownLatch;

import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.widgets.*;
import org.eclipse.swt.*;

import details.Artist;
import details.Cd;
import details.Track;

import runnable_functions.ReferedObject;
import runnable_functions.USERfunctions;

/**
 * Logging Dialog ============== presents stores table for selection by user
 * program is initiated by selected store's view
 */
public class ArtistSearchResultsDialog extends org.eclipse.swt.widgets.Dialog {
	private static Display display;
	private static ArtistSearchResultsDialog inst;
	private static Shell shell;
	private static Shell dialogShell;
	private static Button searchDialogButtonSearchAlbum;
	private static Button searchDialogButtonSearchSong;
	private static Group logDialogGroup;
	private static Button delete;
	private static Button update;

	private static Text searchDialogAlbumTitleText;
	private static Text searchDialogAlbumPerformerText;
	private static Text searchDialogAlbumGenreText;
	private static Text searchDialogAlbumYearFromText;
	private static Text searchDialogAlbumYearToText;

	private static Text searchDialogSongTitleText;
	private static Text searchDialogSongArtistText;
	private static Text searchDialogSongLyricText;
	private static Artist desireArtist;
	Color color = new Color(display, 8, 164, 199);

	public static boolean successful = false;

	static Image background = new Image(display,Main.class.getResourceAsStream("headphone-music2.jpg"));
	static Image background2 = new Image(display,Main.class.getResourceAsStream("headphone-music2.jpg"));
	// Search results group
	private static Group searchGroupResults;
	private static Table searchTableAlbumResults;
	private static TableColumn searchTableColumnAlbum;
	private static TableColumn searchTableColumnArtist;
	private static TableColumn searchTableColumnYear;
	private static TableColumn searchTableColumnGenre;

	//Album Details
	private static Group searchGroupDetails;
	private static Table searchTableSongResults;
	private static TableColumn searchTableColumnSongTrack;
	private static TableColumn searchTableColumnSongName;
	private static TableColumn searchTableColumnSongLength;
	private static Button searchButtonShowLyric;

	// Search options group
	private static Group searchGroupOptions;
	private static Button searchCheckBoxArtist;
	private static Text searchTextBoxArtist;
	private static Button searchCheckBoxSong;
	private static Text searchTextBoxSong;
	private static Button searchCheckBoxAlbum;
	private static Text searchTextBoxAlbum;
	private static Button searchCheckBoxLyric;
	private static Text searchTextBoxLyric;
	private static Button searchCheckBoxYear;
	private static Text searchTextBoxYear;
	private static Button searchCheckBoxGenre;
	private static Text searchTextBoxGenre;
	private static Button searchButtonClear;
	private static Button searchButtonSearch;
	private static Image cDcover;
	private static Button searchButtonShowSongs;
	private static ArrayList<Artist> searchResult;
	private static String[] columnNames = {"Title", "Artist", "Genre", "Year"};



	/**
	 * Auto-generated main method to display this org.eclipse.swt.widgets.Dialog
	 * inside a new Shell.
	 */
	public static void openSearchDialog() {
		try {
			display = Display.getDefault();
			shell = new Shell(display);
			shell.setLocation(Main.getCentral_x() - 406,
					Main.getCentral_y() - 458);
			shell.setSize(398, 200);
			//shell.setBackgroundImage(background);
			inst = new ArtistSearchResultsDialog(shell, SWT.NULL);
			inst.open();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public static void closeInitDialog() {
		shell.close();
	}

	public ArtistSearchResultsDialog(Shell parent, int style) {
		super(parent, style);
	}

	public void open() {
		try {
			Shell parent = getParent();
			dialogShell = new Shell(parent, SWT.DIALOG_TRIM
					| SWT.APPLICATION_MODAL);

			dialogShell.setText("Music Box");
			//dialogShell.setBackgroundImage(background);
			dialogShell.setLayout(null);
			{
				{
					searchGroupResults = new Group(dialogShell, SWT.NONE);
					searchGroupResults.setLayout(null);
					searchGroupResults.setBackgroundImage(background);
					//searchGroupResults.setText("Search Results");
					searchGroupResults.setBounds(0, -8, 690, 635);
					{
						searchTableAlbumResults = new Table(searchGroupResults,
								SWT.BORDER | SWT.FULL_SELECTION | SWT.H_SCROLL
								| SWT.V_SCROLL | SWT.SINGLE); // single row
						// selection
						searchTableAlbumResults.setBounds(200, 23, 300, 280);
						searchTableAlbumResults.setHeaderVisible(true);
						searchTableAlbumResults.setLinesVisible(true);


						searchTableColumnAlbum = new TableColumn(
								searchTableAlbumResults, SWT.NONE);
						searchTableColumnAlbum.setText("Name");
						searchTableColumnAlbum.setResizable(true);
						searchTableColumnAlbum.setMoveable(true);
						searchTableColumnAlbum.setWidth(255);

						
						
						delete = new Button(searchGroupResults,
								SWT.PUSH | SWT.CENTER);
						delete.setText("Delete Artist");
						delete.setBounds(23, 177, 100, 30);
						delete.addListener(SWT.Selection,
								new Listener() {
							public void handleEvent(Event event) {
								int index = searchTableAlbumResults.getSelectionIndex();
								desireArtist =  Main.guiInt.getArtists().get(index);
								
								
								Main.signal = new CountDownLatch(1);
								Main.dbGui.delete(ReferedObject.ARTIST, Integer.toString(desireArtist.getIdNum()), Main.getUser().getUserName());
								try {
									Main.signal.await();
								} catch (InterruptedException e) {
									MessageBox messageBox = new MessageBox(
											ArtistSearchResultsDialog.shell, 32);
									messageBox.setText("Error");
									messageBox.setMessage("Error: "
											+ e.getLocalizedMessage());
									messageBox.open();
									e.printStackTrace();
								}

								String guiErrorMsg = Main.guiInt.getMassage();
								if (guiErrorMsg != null){
									MessageBox messageBox = new MessageBox(
											ArtistSearchResultsDialog.shell, 32);
									messageBox.setText("Song Search");
									messageBox.setMessage("Error: "
											+ guiErrorMsg);
									messageBox.open();
								}else{	
									TableItem[] items = searchTableAlbumResults.getSelection();
									if (items.length == 0) return;
									items[0].dispose();
									MessageBox messageBox = new MessageBox(
											ArtistSearchResultsDialog.shell, 32);
									messageBox.setText("Artist Search");
									messageBox.setMessage("Artist deleted seccesesfuly");
									messageBox.open();
								}
							}
						});
							
					
					}
						searchResult = Main.guiInt.getArtists();
						for (int i = 0; i < searchResult.size(); i++) {
							newEntry(searchResult.get(i).getName());

						}

					}
					{
						searchButtonShowSongs = new Button(searchGroupResults,
								SWT.PUSH | SWT.CENTER);
						searchButtonShowSongs.setText("Show Artist Details");
						searchButtonShowSongs.setBounds(200, 301, 300, 33);
						searchButtonShowSongs.addListener(SWT.Selection,
								new Listener() {
							public void handleEvent(Event event) {
								if (searchTableAlbumResults.getSelectionIndex()!= -1){
								int index = searchTableAlbumResults.getSelectionIndex();
								desireArtist =  Main.guiInt.getArtists().get(index);
								
									TableItem[] items = searchTableSongResults.getItems();
									for (int i = 0; i < items.length ; i++) {
									if (items.length == 0) break;
									items[i].dispose();
									}
								
									Main.signal = new CountDownLatch(1);
									Main.dbGui.getArtistAllInfo(desireArtist.getIdNum());
									try {
										Main.signal.await();
									} catch (InterruptedException e) {
										MessageBox messageBox = new MessageBox(
												ArtistSearchResultsDialog.shell, 32);
										messageBox.setText("Error");
										messageBox.setMessage("Error: "
												+ e.getLocalizedMessage());
										messageBox.open();
										e.printStackTrace();
									}

									String guiErrorMsg = Main.guiInt.getMassage();
									if (guiErrorMsg != null){
										MessageBox messageBox = new MessageBox(
												ArtistSearchResultsDialog.shell, 32);
										messageBox.setText("Artist Search");
										messageBox.setMessage("Error: "
												+ guiErrorMsg);
										messageBox.open();
									}else{
										
									//update.setBounds(23, 140, 100, 30);
									delete.setBounds(23, 177, 100, 30);
									desireArtist = Main.guiInt.getArtist();
									ArrayList<Cd> desireCds = desireArtist.getCds();

									for (int i = 0; i < desireCds.size(); i++) {
										newSongEntry(desireCds.get(i).getTitle(),
												desireCds.get(i).getGenre(),
												desireCds.get(i).getReleaseYear());
									
									}
									}
//									Group cd_cover = new Group(searchGroupDetails,
//											SWT.PUSH | SWT.CENTER);
//									cd_cover.setText("Cd Cover");
//									cd_cover.setBounds(13, 177, 340, 30);
//									if (Main.guiInt.getCdCover() != null){
//										cDcover = Main.guiInt.getCdCover();
//									}else{
//										cDcover = new Image(display,Main.class.getResourceAsStream("no-cover.jpg"));
//										
//									}
//									cd_cover.setBackgroundImage(cDcover);
										


								}
							}
						}
								);
					
				


//				{
//					searchGroupDetails = new Group(dialogShell, SWT.NONE | SWT.NO_BACKGROUND);
//					searchGroupDetails.setLayout(null);
//					searchGroupDetails.setBackgroundImage(background2);
//					searchGroupDetails.setText("List of artist Cds:");
//					searchGroupDetails.setBounds(5, 215, 787, 350);
					{
						searchTableSongResults = new Table(searchGroupResults,
								SWT.BORDER | SWT.FULL_SELECTION | SWT.H_SCROLL
								| SWT.V_SCROLL | SWT.SINGLE);
						searchTableSongResults.setBounds(23, 350, 430, 280);
						searchTableSongResults.setHeaderVisible(true);
						searchTableSongResults.setLinesVisible(true);
						
						searchTableColumnSongTrack = new TableColumn(
								searchTableSongResults, SWT.NONE);
						searchTableColumnSongTrack.setText("Disk title");
						searchTableColumnSongTrack.setResizable(true);
						searchTableColumnSongTrack.setMoveable(true);
						searchTableColumnSongTrack.setWidth(190);


						searchTableColumnSongTrack = new TableColumn(
								searchTableSongResults, SWT.NONE);
						searchTableColumnSongTrack.setText("Genre");
						searchTableColumnSongTrack.setResizable(true);
						searchTableColumnSongTrack.setMoveable(true);
						searchTableColumnSongTrack.setWidth(140);

						searchTableColumnSongName = new TableColumn(
								searchTableSongResults, SWT.NONE);
						searchTableColumnSongName.setText("Released Year");
						searchTableColumnSongName.setResizable(true);
						searchTableColumnSongName.setMoveable(true);
						searchTableColumnSongName.setWidth(95);


//						searchTableColumnSongLength = new TableColumn(
//								searchTableSongResults, SWT.NONE);
//						searchTableColumnSongLength.setText("Duration");
//						searchTableColumnSongLength.setResizable(true);
//						searchTableColumnSongLength.setMoveable(true);
//						searchTableColumnSongLength.setWidth(60);
						
						
						
						
						
						
						
//						{
//							searchButtonShowLyric = new Button(searchGroupResults,
//									SWT.PUSH | SWT.CENTER);
//							searchButtonShowLyric.setText("Get Album details");
//							searchButtonShowLyric.setBounds(333, 315, 430, 30);
//						}

					}

				}
			}
			dialogShell.layout();
			dialogShell.pack();
			dialogShell.setLocation(getParent().toDisplay(100, 100));
			dialogShell.open();
			Display display = dialogShell.getDisplay();


			while (!dialogShell.isDisposed()) {
				if (!display.readAndDispatch())
					display.sleep();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}


	public static Shell getDialogShell() {
		return dialogShell;
	}

	public static void setDialogShell(Shell dialogShell) {
		ArtistSearchResultsDialog.dialogShell = dialogShell;
	}

	//	public static Button getInitDialogButtonExit() {
	//		return logDialogButtonExit;
	//	}
	//
	//	public static void setInitDialogButtonExit(Button logDialogButtonExit) {
	//		SearchDialog.logDialogButtonExit = logDialogButtonExit;
	//	}
	//
	//	public static Button getInitDialogButtonStart() {
	//		return logDialogButtonStart;
	//	}
	//
	//	public static void setInitDialogButtonStart(Button logDialogButtonStart) {
	//		SearchDialog.logDialogButtonStart = logDialogButtonStart;
	//	}

	public static Group getInitDialogGroup() {
		return logDialogGroup;
	}

	public static void setInitDialogGroup(Group logDialogGroup) {
		ArtistSearchResultsDialog.logDialogGroup = logDialogGroup;
	}

	public static Display getInitDisplay() {
		return display;
	}

	public static void setDisplay(Display display) {
		ArtistSearchResultsDialog.display = display;
	}

	public static Display getDisplay() {
		return display;
	}

	public static ArtistSearchResultsDialog getInst() {
		return inst;
	}

	public static Shell getShell() {
		return shell;
	}


	/**
	 * clear search fields
	 */
	protected static void clearSearchFields() {
		searchTextBoxArtist.setText("");
		searchTextBoxAlbum.setText("");
		searchTextBoxSong.setText("");
		searchTextBoxLyric.setText("");
		searchTextBoxGenre.setText("");
		searchTextBoxYear.setText("");
	}
	private void newEntry(String name) {
		String[] data = {name};
		if (data != null) {
			TableItem item = new TableItem(searchTableAlbumResults, SWT.NONE);
			item.setText(data);
		}
	}
	private void newSongEntry( String title, String artist,  int year) {
		String[] data = {title,artist,Integer.toString(year)};
		if (data != null) {
			TableItem item = new TableItem(searchTableSongResults, SWT.NONE);
			item.setText(data);
		}
	}
}
